/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.Priority;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.RollingFileAppender;
import sagex.api.Configuration;
import sagex.api.Global;

/**
 *
 * @author jphipps
 */
public class logger extends ortusvars {
	Logger logger = null;



	public logger() {
		File ortuslog = new File("ortus.log");
		if ( ortuslog.exists())
			ortuslog.delete();
		logger = Logger.getLogger("ortus.com");
//		PropertyConfigurator.configure("ortus-log4j.properties");
		logger.removeAllAppenders();
		RollingFileAppender rollingfile = null;
		try {
			rollingfile = new RollingFileAppender(new PatternLayout("%d [%C@%t] %m%n"), "ortus.log",true);
		} catch ( IOException ex) {
			System.out.println("ortuslog: Failed to create rolling log");
		}
		rollingfile.setMaxFileSize("10MB");
		rollingfile.setMaxBackupIndex(4);
		logger.addAppender(rollingfile);
		
		FileAppender fileappender = null;
		try {
			fileappender = new FileAppender(new PatternLayout("%d [%C@%t] %m%n"), "sage_tv.0", true);
		} catch (IOException ex) {
			System.out.println("ortuslog: Failed to create logger instance");
		}
		logger.addAppender(fileappender);

		int LogLevelInt = Integer.parseInt(ortustve.api.GetSageProperty("ortus/log/log4j_level", "7"));

		switch(LogLevelInt) {
			case 0: logger.setLevel(Level.OFF); break;
			case 1: logger.setLevel(Level.FATAL); break;
			case 2: logger.setLevel(Level.ERROR); break;
			case 3: logger.setLevel(Level.WARN); break;
			case 4: logger.setLevel(Level.INFO); break;
			case 5: logger.setLevel(Level.DEBUG); break;
			case 6: logger.setLevel(Level.TRACE); break;
			default: logger.setLevel(Level.ALL);
		}
		logger.info("ortuslog: Logging Started");

	}

	public void SetSageAll() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "7");
	}
	public void SetSageTrace() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "6");
	}
	public void SetSageDebug() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "5");
	}
	public void SetSageInfo() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "4");
	}
	public void SetSageWarn() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "3");
	}
	public void SetSageError() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "2");
	}
	public void SetSageFatal() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "1");
	}
	public void SetSageOff() {
		ortustve.api.SetSageProperty("ortus/log/sage_level", "0");
	}
	public void SetLog4jAll() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "7");
		logger.setLevel(Level.ALL);
	}
	public void SetLog4jTrace() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "6");
		logger.setLevel(Level.TRACE);
	}
	public void SetLog4jDebug() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "5");
		logger.setLevel(Level.DEBUG);
	}
	public void SetLog4jInfo() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "4");
		logger.setLevel(Level.INFO);
	}
	public void SetLog4jWarn() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "3");
		logger.setLevel(Level.WARN);
	}
	public void SetLog4jError() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "2");
		logger.setLevel(Level.ERROR);
	}
	public void SetLog4jFatal() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "1");
		logger.setLevel(Level.FATAL);
	}
	public void SetLog4jOff() {
		ortustve.api.SetSageProperty("ortus/log/log4j_level", "0");
		logger.setLevel(Level.OFF);
	}

	public void SetDebugLevel(Level debuglevel) {
		logger.setLevel(debuglevel);
	}

	public Logger getLogger() {
		return logger;
	}

	public void DebugLog(Object MsgLevel, String MsgString) {
		

		int MsgLevelInt = 0;
		String MsgLevelStr = new String();

		if (MsgLevel instanceof String) {
			MsgLevelStr = MsgLevel.toString().toUpperCase();
			if (MsgLevelStr.equals("TRACE2")) {
				MsgLevelInt = 6;
			} else if (MsgLevelStr.equals("TRACE")) {
				MsgLevelInt = 6;
			} else if (MsgLevelStr.equals("DEBUG")) {
				MsgLevelInt = 5;
			} else if (MsgLevelStr.equals("INFO")) {
				MsgLevelInt = 4;
			} else if (MsgLevelStr.equals("WARNING")) {
				MsgLevelInt = 3;
			} else if (MsgLevelStr.equals("ERROR")) {
				MsgLevelInt = 2;
			} else if (MsgLevelStr.equals("FATAL")) {
				MsgLevelInt = 1;
			} else {
				try {
					MsgLevelInt = Integer.parseInt(MsgLevelStr);
				} catch (NumberFormatException nfe) {
					DebugLog(WARNING, "DebugLog MsgLevel Format Exception: " + nfe.getMessage());
					return;
				}
			}
		} else if (MsgLevel instanceof Integer) {
			MsgLevelInt = (Integer) MsgLevel;
		} else {
			ortustve.api.DebugLog(WARNING, "DebugLog MsgLevel Format Exception: MsgLevel is not String or Integer");
			return;
		}

		if (Integer.parseInt(Configuration.GetProperty("ortus/log/log4j_level", "6")) > 0) {
			switch((Integer)MsgLevelInt) {
				case 1: logger.fatal(MsgString);
					break;
				case 2: logger.error(MsgString);
					break;
				case 3: logger.warn(MsgString);
					break;
				case 4: logger.info(MsgString);
					break;
				case 5: logger.debug(MsgString);
					break;
				case 6: logger.trace(MsgString);
					break;
			}
		}

		int LogLevelInt = Integer.parseInt(Configuration.GetProperty("ortus/log/sage_level", "6"));
		if (LogLevelInt > 0){
			if (MsgLevelInt == 6) {
				MsgLevelStr = "TRACE:   ";
			} else if (MsgLevelInt == 5) {
				MsgLevelStr = "DEBUG:   ";
			} else if (MsgLevelInt == 4) {
				MsgLevelStr = "INFO:    ";
			} else if (MsgLevelInt == 3) {
				MsgLevelStr = "WARNING: ";
			} else if (MsgLevelInt == 2) {
				MsgLevelStr = "ERROR:   ";
			} else if (MsgLevelInt == 1) {
				MsgLevelStr = "FATAL:   ";
			}

			if (MsgLevelInt <= LogLevelInt) {
				Global.DebugLog("Ortus " + MsgLevelStr + MsgString);
			}
		}
		return;
	}

	public static void DebugLogDB(Object MsgLevel, String MsgString) {

		int MsgLevelInt = 0;
		String MsgLevelStr = new String();

		if (MsgLevel instanceof String) {
			MsgLevelStr = MsgLevel.toString().toUpperCase();
			if (MsgLevelStr.equals("TRACE2")) {
				MsgLevelInt = 5;
			} else if (MsgLevelStr.equals("TRACE")) {
				MsgLevelInt = 4;
			} else if (MsgLevelStr.equals("INFO")) {
				MsgLevelInt = 3;
			} else if (MsgLevelStr.equals("WARNING")) {
				MsgLevelInt = 2;
			} else if (MsgLevelStr.equals("ERROR")) {
				MsgLevelInt = 1;
			} else {
				try {
					MsgLevelInt = Integer.parseInt(MsgLevelStr);
				} catch (NumberFormatException nfe) {
					api.DebugLog(WARNING, "DebugLog MsgLevel Format Exception: " + nfe.getMessage());
					return;
				}
			}
		} else if (MsgLevel instanceof Integer) {
			MsgLevelInt = (Integer) MsgLevel;
		} else {
			api.DebugLog(WARNING, "DebugLog MsgLevel Format Exception: MsgLevel is not String or Integer");
			return;
		}

		ortustve.api.executeSQL("insert into sage.syslog ( event_level, event_time, event_msg) values ( " + MsgLevelInt + ",current_timestamp,'" + MsgString + "'");

		return;
	}

	public static List<Object> GetDebugLogDB(int MsgLevel) {
		String sql = "select event_time, event_level, event_msg from sage.syslog";
		if ( MsgLevel < 9)
			sql+=" where event_level = " + MsgLevel;
		return ortustve.api.executeSQLQuery(sql);
	}
}
